package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("")
    @RequiresPermissions("employee:index")
    @PermissionName("员工列表")
    public String index() {
        //只负责跳转到employee.jsp页面即可
        return "employee";
    }
    @RequestMapping("list")
    @RequiresPermissions("employee:list")
    @PermissionName("员工数据")
    @ResponseBody
    public PageResult list(EmployeeQueryObject qo) {
        //获取当前用户
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        //判断当前用户是否含有HR这个角色
        if(!SecurityUtils.getSubject().hasRole("HR")){
            //不是该角色则设置只能查询该用户的id值
            qo.setCurrentUserId(employee.getId());
        }
        PageResult result = employeeService.query(qo);

        return result;
    }

    @RequestMapping("save")
    @RequiresPermissions("employee:save")
    @PermissionName("员工保存")
    @ResponseBody
    public JsonResult save(Employee employee) {
        JsonResult result = new JsonResult();
        try{
         employeeService.insert(employee);
         result.setMessage("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("保存失败");
        }

        return result;
    }
<<<<<<< HEAD
    @RequestMapping("changePassword")
    @ResponseBody
    public JsonResult changePassword(Long empId,String password) {
        JsonResult result = new JsonResult();
        try{
            employeeService.changePassword(empId,password);
            result.setMessage("修改密码成功");

        }catch (Exception e){
            e.printStackTrace();
            result.mark("修改密码失败");
        }
        return result;
    }
=======
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
    @RequestMapping("edit")
    @RequiresPermissions("employee:edit")
    @PermissionName("员工编辑")
    @ResponseBody
    public JsonResult edit(Employee employee) {
        JsonResult result = new JsonResult();
        try{
         employeeService.updateByPrimaryKey(employee);
        result.setMessage("编辑成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("编辑失败");
        }

        return result;
    }
    @RequestMapping("quit")
    @RequiresPermissions("employee:quit")
    @PermissionName("员工离职")
    @ResponseBody
    public JsonResult quit(Long id) {
        JsonResult result = new JsonResult();
        try{
         employeeService.updateState(id);
        result.setMessage("离职成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("离职失败");
        }
<<<<<<< HEAD
        return result;

    }
    @RequestMapping("goWork")
    @ResponseBody
    public JsonResult goWork(Long empId) {
        JsonResult result = new JsonResult();
        try{
         employeeService.goWork(empId);
        result.setMessage("打卡成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("打卡失败");
        }
        return result;

    }
    @RequestMapping("backWork")
    @ResponseBody
    public JsonResult backWork(Long empId) {
        JsonResult result = new JsonResult();
        try{
         employeeService.backWork(empId);
        result.setMessage("打卡成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("打卡失败");
        }
        return result;

=======

        return result;
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
    }
    @RequestMapping("export")
    @RequiresPermissions("employee:export")
    @PermissionName("员工列表导出")
    @ResponseBody
    public void export(EmployeeQueryObject qo, HttpServletResponse response)throws  Exception {
        //1.设置查询一页查询的数据多少
        qo.setRows(1000);
        //查询出数据
        List<Employee> list = employeeService.querForList(qo);
        //设置请求头的信息
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        OutputStream outputStream = response.getOutputStream();

        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
        //创建一个名employee的sheet
        WritableSheet sheet = workbook.createSheet("employee", 0);
        Label userNameLabel = new Label(0, 0, "用户名");
        Label realNameLabel = new Label(1, 0, "真实姓名");
        Label passwordLabel = new Label(2, 0, "密码");
        Label telLabel = new Label(3, 0, "电话号码");
        Label emailLabel = new Label(4, 0, "邮箱");
        Label deptLabel = new Label(5, 0, "部门");
        Label inputTimeLabel = new Label(6, 0, "入职时间");
        Label stateLabel = new Label(7, 0, "状态");
        Label adminLabel = new Label(8, 0, "管理员");
        //将数据填写到单元格中
        sheet.addCell(userNameLabel);
        sheet.addCell(realNameLabel);
        sheet.addCell(passwordLabel);
        sheet.addCell(telLabel);
        sheet.addCell(emailLabel);
        sheet.addCell(deptLabel);
        sheet.addCell(inputTimeLabel);
        sheet.addCell(stateLabel);
        sheet.addCell(adminLabel);
        //循环迭代出所有的Employee对象
        for (int index = 0;index <list.size();index++){
            Employee employee = list.get(index);
            int row = index + 1;
            Label tempuserNameLabel = new Label(0, row, employee.getUserName());
            Label temprealNameLabel = new Label(1, row, employee.getRealName());
            Label temppasswordLabel = new Label(2, row, employee.getPassword());
            Label temptelLabel = new Label(3, row, employee.getTel());
            Label tempemailLabel = new Label(4, row, employee.getEmail());
            Label tempdeptLabel = new Label(5, row, employee.getDept().getName());
            Label tempinputTimeLabel = new Label(6, row, employee.getInputTime().toLocaleString());
            String stateString = employee.getState() == 0?"在职":"离职";
            Label tempstateLabel = new Label(7, row,stateString);
            String adminString = employee.isAdmin()?"是":"否";
            Label tempadminLabel = new Label(8, row,adminString);

            sheet.addCell(tempuserNameLabel);
            sheet.addCell(temprealNameLabel);
            sheet.addCell(temppasswordLabel);
            sheet.addCell(temptelLabel);
            sheet.addCell(tempemailLabel);
            sheet.addCell(tempdeptLabel);
            sheet.addCell(tempinputTimeLabel);
            sheet.addCell(tempstateLabel);
            sheet.addCell(tempadminLabel);

        }
        //下载文件,将文件写入流中
        workbook.write();
        //释放资源
        workbook.close();

    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<Employee> listAll(EmployeeQueryObject qo) {

        List<Employee> list = employeeService.selectAll();

        return list;
    }
}